import 'package:flutter/material.dart';

enum TransactionType { income, expense }

class TransactionForm extends StatefulWidget {
  const TransactionForm({Key? key}) : super(key: key);

  @override
  _TransactionFormState createState() => _TransactionFormState();
}

class _TransactionFormState extends State<TransactionForm> {
  final TextEditingController _amountController = TextEditingController();
  TransactionType _transactionType = TransactionType.expense;

  @override
  void dispose() {
    _amountController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('记一笔'),
        leading: IconButton(
          icon: const Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: Column(
        children: [
          // 金额显示区域
          Container(
            padding: const EdgeInsets.symmetric(vertical: 20),
            child: Text(
              _amountController.text.isEmpty ? '0.00' : _amountController.text,
              style: const TextStyle(
                fontSize: 24, // 减小字体大小
                fontWeight: FontWeight.bold,
              ),
            ),
          ),
          // 分类选择区域
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 16),
            child: Row(
              children: [
                Expanded(
                  child: ElevatedButton(
                    onPressed: () => _selectCategory(context, true),
                    style: ElevatedButton.styleFrom(
                      backgroundColor:
                          _transactionType == TransactionType.expense
                              ? Colors.blue
                              : Colors.grey[300],
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                    child: Text(
                      '支出',
                      style: TextStyle(
                        color:
                            _transactionType == TransactionType.expense
                                ? Colors.white
                                : Colors.black,
                      ),
                    ),
                  ),
                ),
                const SizedBox(width: 16),
                Expanded(
                  child: ElevatedButton(
                    onPressed: () => _selectCategory(context, false),
                    style: ElevatedButton.styleFrom(
                      backgroundColor:
                          _transactionType == TransactionType.income
                              ? Colors.blue
                              : Colors.grey[300],
                      padding: const EdgeInsets.symmetric(vertical: 12),
                    ),
                    child: Text(
                      '收入',
                      style: TextStyle(
                        color:
                            _transactionType == TransactionType.income
                                ? Colors.white
                                : Colors.black,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
          const SizedBox(height: 20),
          // 数字键盘
          Expanded(
            child: GridView.count(
              crossAxisCount: 3,
              childAspectRatio: 1.5, // 调整按钮宽高比
              padding: const EdgeInsets.all(16),
              mainAxisSpacing: 16, // 增加垂直间距
              crossAxisSpacing: 16, // 增加水平间距
              children: [
                _buildNumberButton('1'),
                _buildNumberButton('2'),
                _buildNumberButton('3'),
                _buildNumberButton('4'),
                _buildNumberButton('5'),
                _buildNumberButton('6'),
                _buildNumberButton('7'),
                _buildNumberButton('8'),
                _buildNumberButton('9'),
                _buildNumberButton('.'),
                _buildNumberButton('0'),
                _buildNumberButton('⌫'),
              ],
            ),
          ),
          // 保存按钮
          Container(
            width: double.infinity,
            padding: const EdgeInsets.all(16),
            child: ElevatedButton(
              onPressed: _saveTransaction,
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.blue,
                padding: const EdgeInsets.symmetric(vertical: 16),
              ),
              child: const Text(
                '保存',
                style: TextStyle(fontSize: 18, color: Colors.white),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildNumberButton(String text) {
    bool isOperator = ['+', '-', '×', '÷'].contains(text);
    return InkWell(
      onTap: () {
        if (isOperator) {
          // 处理运算符
          if (text == '+') {
            setState(() {
              _transactionType = TransactionType.income;
            });
          } else if (text == '-') {
            setState(() {
              _transactionType = TransactionType.expense;
            });
          }
        } else {
          // 处理数字和小数点
          String currentText = _amountController.text;
          if (text == '.') {
            if (!currentText.contains('.')) {
              if (currentText.isEmpty) {
                _amountController.text = '0.';
              } else {
                _amountController.text = '$currentText.';
              }
            }
          } else {
            if (currentText == '0' && text != '00') {
              _amountController.text = text;
            } else {
              _amountController.text = '$currentText$text';
            }
          }
        }
      },
      child: Container(
        margin: const EdgeInsets.all(4),
        decoration: BoxDecoration(
          color: isOperator ? Colors.grey[200] : Colors.white,
          borderRadius: BorderRadius.circular(8),
          border: Border.all(color: Colors.grey[300]!, width: 1),
        ),
        child: Center(
          child: Text(
            text,
            style: TextStyle(
              fontSize: 24,
              color: isOperator ? Colors.blue : Colors.black87,
              fontWeight: isOperator ? FontWeight.bold : FontWeight.normal,
            ),
          ),
        ),
      ),
    );
  }
}
